/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeriesPropertiesDialog.java
 *
 * Created on 2012-02-26, 21:01:38
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JLabel;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public class SeriesPropertiesDialog extends ChartPanelDialog{

    private ExtendableChartPanel chartPanel;

    public SeriesPropertiesDialog(ExtendableChartPanel extendableChartPanel) {
        super(extendableChartPanel);
        initComponents();
        setLocationRelativeTo(extendableChartPanel);
        this.chartPanel = extendableChartPanel;
        jPanel1.setSize(jPanel1.getWidth(), 500);
        jPanel1.revalidate();
        loadSeries();
        jPanel1.revalidate();
        jPanel1.repaint();
    }

    private void loadSeries() {
        int i = -1;
        DatasetCollection collection = chartPanel.getDatasetCollection();
     //   for (Entry<Integer, XYReggresionSeriesCollection> entry : chartPanel.getDataSeriesCollections().entrySet()) {
            for (XYIntervalSeries series : collection.getSeriesList()) {
                addRowAt((String) series.getKey(), ++i);
                Dimension dim = jPanel1.getPreferredSize();
                dim.height += 50;
                jPanel1.setPreferredSize(dim);
                jPanel1.repaint();
                jPanel1.revalidate();
            }
       // }
        mainScrollPane.repaint();
    }

    private void addRowAt(String seriesName, int pos) {
        createLabelForRow(seriesName, pos);
        createButtonForRow(seriesName, pos);
    }

    private JLabel createLabelForRow(String label, int pos) {
        JLabel seriesLabelName = new JLabel();
        seriesLabelName.setText(label);
        jPanel1.add(seriesLabelName);
        seriesLabelName.setBounds(10, 11 + 50 * (pos), 137, 30);
        return seriesLabelName;
    }

    private JButton createButtonForRow(final String label, final int pos) {
        JButton propertiesButton = new JButton();
        propertiesButton.setText("Set Properties...");
        jPanel1.add(propertiesButton);
        propertiesButton.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                new SeriesDetailsDialog(label, pos, chartPanel, SeriesPropertiesDialog.this).setVisible(true);

            }
        });
        propertiesButton.setBounds(150, 11 + 50 * (pos), 129, 32);
        return propertiesButton;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainScrollPane = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        seriesPropertiesLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        mainScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        mainScrollPane.setAutoscrolls(true);

        jPanel1.setLayout(null);
        mainScrollPane.setViewportView(jPanel1);

        seriesPropertiesLabel.setText("Set properties for series");

        closeButton.setText("Close");
        closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                closeButtonMouseClicked(evt);
            }
        });
        jPanel2.add(closeButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mainScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                    .addComponent(seriesPropertiesLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(seriesPropertiesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_closeButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_closeButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane mainScrollPane;
    private javax.swing.JLabel seriesPropertiesLabel;
    // End of variables declaration//GEN-END:variables
}
